import logging

import requests
import config.path_cnf as config
import config.params_cnf as params_cnf


class OrderApi(object):

    def __init__(self):
        self.order_by_user_url = config.EGO_URL + config.ORDER_BY_USER
        self.add_order_url = config.EGO_URL + config.ADD_ORDER
        self.query_order_url = config.EGO_URL + config.QUERY_ORDER

    # 获取用户订单列表
    def order_by_user(self, page):
        return requests.get(self.order_by_user_url, params={"page": str(page)}, headers=params_cnf.TOKEN_BODY)

    # 创建订单
    def add_order(self, data):
        return requests.post(self.add_order_url, json={'products': data}, headers=params_cnf.TOKEN_BODY)

    # 查看订单(路径参数)
    def query_order(self, index):
        return requests.get(self.query_order_url + str(index), headers=params_cnf.TOKEN_BODY)


if __name__ == '__main__':
    # 获取用户订单列表
    token = '032fd5fc0e5c25af3fe461bff0392df4'
    res = OrderApi().order_by_user('3')
    print(res)
    print(len(res.json().get('data')))

    # # 创建订单
    order_data = [{"product_id": 8, "count": 1}, {"product_id": 10, "count": 2}]
    print(OrderApi().add_order(order_data))

    # 查看订单
    print(OrderApi().query_order('33'))
